package com.stores.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.stores.action.ManageStoresAction;
import com.stores.model.Address;
import com.stores.model.PhoneNumber;
import com.stores.model.Store;
import com.stores.toolset.InputValidator;
import com.stores.toolset.WebAction;
import com.stores.viewModel.StoreViewModel;

public class StoresController extends HttpServlet {

	private static final String ADMIN_STORE_SAVE = "/admin/store/save";
	private static final String ADMIN_STORE_LOAD_SINGLE = "/admin/store/load/single";
	private static final String ADMIN_STORE_LOAD = "/admin/store/load";
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/***
	 * 
	 */
	@SuppressWarnings("static-access")
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		ServletContext context = getServletContext();
		String requestUrl = request.getRequestURL().toString();
		if (requestUrl.indexOf("manage-stores") > -1) {
			RequestDispatcher dispatcher = context
					.getRequestDispatcher("/admin/manage-store.jsp");
			try {
				dispatcher.forward(request, response);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
			}
		} else {
			try {
				response.sendError(response.SC_BAD_REQUEST,
						"Valid url is /admin/manage-stores");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
			}
		}
	}

	/***
	 * 
	 */
	@SuppressWarnings("static-access")
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		WebAction webAction = new WebAction(request, response);
		ManageStoresAction manage = new ManageStoresAction();
		InputValidator validator = new InputValidator();

		if (webAction.isUrl(ADMIN_STORE_LOAD)) {
			Gson gson = new Gson();
			webAction.writeResponse(gson.toJson(manage.Retrieve()));
		}
		if (webAction.isUrl(ADMIN_STORE_LOAD_SINGLE)) {
			Gson gson = new Gson();
			String jsonData = webAction.getJson();
			long storeId = gson.fromJson(jsonData, StoreViewModel.class)
					.getStoreId();
			webAction.writeResponse(gson.toJson(manage.Retrieve(storeId)));
		} else {
			String description = request.getParameter("desc");
			String name = request.getParameter("name");
			String house = request.getParameter("house");
			String building = request.getParameter("building");
			String street = request.getParameter("street");
			String locality = request.getParameter("locality");
			String city = request.getParameter("city");
			String number = request.getParameter("number");
			String email = request.getParameter("email");
			String tin = request.getParameter("tin");
			String tagline = request.getParameter("tagline");
			String contactPerson = request.getParameter("contact-person");

			if (validator.isValid(description + name + house + building
					+ street + locality + city + contactPerson + number + email
					+ tin + tagline)) {// club them
				// together
				// and
				// send for
				// validation
				Address primaryAddress = new Address();
				PhoneNumber primaryNumber = new PhoneNumber();

				primaryAddress.setLine1(house);
				primaryAddress.setLine2(street);
				primaryAddress.setBuildingOrPlot(building);
				primaryAddress.setLocality(locality);
				primaryAddress.setCity(city);

				primaryNumber.setNumber(number);

				Store store = new Store();
				store.setDescription(description);
				store.setName(name);
				store.setStoreAddress(primaryAddress);
				store.setStoreNumber(primaryNumber);
				store.setEmailAddress(email);
				store.setTagline(tagline);
				store.setTin(tin);
				store.setContactPersonName(contactPerson);

				if (webAction.isUrl(ADMIN_STORE_SAVE)) {
					String storeId = request.getParameter("store-id");
					if (storeId.length() == 0) {
						manage.Create(store);
					} else {
						manage.Update(store, Long.parseLong(storeId));
					}
				}
			} else {
				try {
					response.sendError(response.SC_BAD_REQUEST,
							"Invalid characters found in the input data. Data validation failed!!");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					// e.printStackTrace();
				}
			}
		}
	}
}
